﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using vuonthieunhi.Models;
using vuonthieunhi.Library;
using System.Data.Common;

namespace vuonthieunhi.Areas.Administration.Models
{
    public class NewsDao : BaseDao
    {
        public List<sp_GetTopNewResult> GetTopNew(int number)
        {
            return dbContext.sp_GetTopNew(number).ToList();
        }

        public List<sp_GetNewsResult> GetList(string text, string author, DateTime? fromDate, DateTime? toDate)
        {
            return dbContext.sp_GetNews(text, author, fromDate, toDate).ToList();
        }

        public New GetByID(int id)
        {
            return dbContext.News.Where(q => q.ID == id).FirstOrDefault();
        }

        private void Delete(New objUI)
        {
            New objDB = GetByID(objUI.ID);
            if (objDB != null)
            {
                objDB.DeleteFlag = true;
                dbContext.SubmitChanges();
            }

        }

        public List<sp_GetNewsResult> Sort(List<sp_GetNewsResult> list, string sortColumn, string sortOrder)
        {
            int order;

            if (sortOrder == "desc")
            {
                order = -1;
            }
            else
            {
                order = 1;
            }
            switch (sortColumn)
            {

                case "ID":
                    list.Sort(
                         delegate(sp_GetNewsResult m1, sp_GetNewsResult m2)
                         { return m1.ID.CompareTo(m2.ID) * order; });
                    break;
                case "Title":
                    list.Sort(
                         delegate(sp_GetNewsResult m1, sp_GetNewsResult m2)
                         { return m1.Title.CompareTo(m2.Title) * order; });
                    break;
                case "PostedDate":
                    list.Sort(
                         delegate(sp_GetNewsResult m1, sp_GetNewsResult m2)
                         { return (m1.PostedDate.HasValue ? m1.PostedDate.Value : DateTime.Now).CompareTo(m2.PostedDate.HasValue ? m2.PostedDate.Value : DateTime.Now) * order; });
                    break;
                case "Author":
                    list.Sort(
                         delegate(sp_GetNewsResult m1, sp_GetNewsResult m2)
                         { return (!string.IsNullOrEmpty(m1.Author) ? m1.Author : string.Empty).CompareTo(!string.IsNullOrEmpty(m2.Author) ? m2.Author : string.Empty) * order; });
                    break;
                case "Description":
                    list.Sort(
                         delegate(sp_GetNewsResult m1, sp_GetNewsResult m2)
                         { return (!string.IsNullOrEmpty(m1.Description) ? m1.Description : string.Empty).CompareTo(!string.IsNullOrEmpty(m2.Description) ? m2.Description : string.Empty) * order; });
                    break;
            }

            return list;
        }

        public Message DeleteList(string ids)
        {
            Message msg = null;
            DbTransaction trans = null;
            bool canDelete = true;
            int total = 0;
            try
            {
                dbContext.Connection.Open();
                trans = dbContext.Connection.BeginTransaction();
                dbContext.Transaction = trans;

                if (!string.IsNullOrEmpty(ids))
                {
                    ids = ids.TrimEnd(',');
                    string[] idArr = ids.Split(',');
                    total = idArr.Count();
                    foreach (string id in idArr)
                    {
                        New objNew = GetByID(int.Parse(id));
                        if (objNew != null)
                        {
                            Delete(objNew);
                        }
                    }
                    if (canDelete)
                    {
                        // Show succes message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, total.ToString() + " tin tức", "xóa");
                        trans.Commit();
                    }
                    else
                    {
                        trans.Rollback();
                    }
                }
            }
            catch (Exception)
            {
                if (trans != null) trans.Rollback();
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }

        public Message Create(New objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    dbContext.News.InsertOnSubmit(objUI);
                    dbContext.SubmitChanges();
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Tin tức mới", "nhập");
                }
            }
            catch
            {
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }

        public Message Update(New objUI)
        {
            Message msg = null;
            try
            {
                New objDb = GetByID(objUI.ID);
                if (objDb != null)
                {
                    objDb.Title = objUI.Title;
                    objDb.Image = objUI.Image;
                    objDb.NewsContent = objUI.NewsContent;
                    objDb.Description = objUI.Description;
                    objDb.PostedDate = objUI.PostedDate;
                    objDb.Author = objUI.Author;
                    dbContext.SubmitChanges();
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Tin tức '"+ objUI.Title + "'", "cập nhật");
                }
            }
            catch
            {
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }
    }
}